#!/bin/sh
# FOSSology dbcreate script
# SPDX-FileCopyrightText: © 2008-2012 Hewlett-Packard Development Company, L.P.

# SPDX-License-Identifier: GPL-2.0-only
#
# This script checks to see if the the fossology db exists and if not
# then creates it.

if [ "$FOSSOLOGY_DB_HOST" ]; then
  echo "NOTE: using external DB on host $FOSSOLOGY_DB_HOST"
  exit 0
fi

echo "*** Setting up the FOSSology database ***"

# At some point this is where we could dynamically set the db password

# first check that postgres is running
psqln=0
until [ "$psqln" -ge 10 ]; do
   su --whitelist-environment="PGHOST" postgres -c 'echo \\q|psql'
   if [ $? = 0 ]; then
      break
   fi
   echo "WARNING: postgresql isn't running. Retrying..."
   sleep 15
   psqln=$((psqln+1))
done

su --whitelist-environment="PGHOST" postgres -c 'echo \\q|psql'
if [ $? != 0 ]; then
   echo "ERROR: postgresql isn't running"
   exit 1
fi

# then check to see if the db already exists
su --whitelist-environment="PGHOST" postgres -c "psql --tuples-only --command \"select * from pg_database where datname = 'fossology';\""|grep fossology >/dev/null 2>&1
if [ $? = 0 ]; then
   echo "NOTE: fossology database already exists, not creating"
   echo "*** Checking for plpgsql support ***"
   su --whitelist-environment="PGHOST" postgres -c 'echo "SELECT * FROM pg_language;" | psql -t fossology' |grep -q plpgsql
   if [ $? = 0 ]; then
      echo "NOTE: plpgsql already exists in fossology database, good"
   else
      echo "NOTE: plpgsql doesn't exist, adding"
      su --whitelist-environment="PGHOST" postgres -c 'echo "CREATE LANGUAGE plpgsql;" | psql fossology'
      if [ $? != 0 ]; then
         echo "ERROR: failed to add plpgsql to fossology database"
      fi
   fi
   echo "*** Checking for 'uuid-ossp' support ***"
   su --whitelist-environment="PGHOST" postgres -c 'echo "SELECT * FROM pg_extension;" | psql -t fossology' |grep -q uuid-ossp
   if [ $? = 0 ]; then
      echo "NOTE: 'uuid-ossp' already exists in fossology database, good"
   else
      echo "NOTE: 'uuid-ossp' doesn't exist, adding"
      su --whitelist-environment="PGHOST" postgres -c 'echo "CREATE EXTENSION \"uuid-ossp\";" | psql fossology'
      if [ $? != 0 ]; then
         echo "ERROR: failed to add 'uuid-ossp' to fossology database"
      fi
   fi
else
   echo "*** Initializing database ***"
   su --whitelist-environment="PGHOST" postgres -c psql < @FO_LIBEXECDIR@/fossologyinit.sql
   if [ $? != 0 ] ; then
      echo "ERROR: Database failed during configuration."
      exit 1
   fi
fi
